#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums)
    {
        vector<vector<int>> res;
        vector<int> subres(3);
        int n = nums.size();
        int head = 0;
        int end = n - 1;
        int mid = head + 1;

        sort(nums.begin(), nums.end());
        int tmph = 0;
        while (head < n)
        {
            tmph = nums[head];
            while (mid < end)
            {
                if (nums[mid] + nums[end] > (-nums[head]))
                {
                    end--;
                }
                else if (nums[mid] + nums[end] == (-nums[head]))
                {
                    subres[0] = (nums[head]);
                    subres[1] = (nums[mid]);
                    subres[2] = (nums[end]);

                    res.push_back(subres);

                    while (mid<n && nums[mid] == subres[1])
                    {
                        mid++;
                    }

                    while (end >=0 && nums[end] == subres[2])
                    {
                        end--;
                    }
                }
                else
                {
                    mid++;
                }
            }

            while (head< n && nums[head] == tmph)
            {
                head++;
            }
            mid = head + 1;
            end = n - 1;
        }
        return res;
    }
};

int main()
{
    Solution s1;
    vector<int> v1{ 0,0,0};
    s1.threeSum(v1);
    return 0;
}